package keeto.com.vn.classified.ui;
import java.util.Hashtable;
import java.util.Vector;

import keeto.com.vn.classfied.model.Area;
import keeto.com.vn.classfied.model.Category;
import keeto.com.vn.classified.adapter.ProductCategoriesAdapterManager;
import keeto.com.vn.classified.database.BuiltinService;
import keeto.com.vn.classified.database.BuiltinServiceManager;
import keeto.com.vn.classified.manager.IAManager;
import keeto.com.vn.classified.menutab.IMenuTabConfig;
import keeto.com.vn.classified.menutab.MenuTabAdapter;
import keeto.com.vn.classified.widgets.LogoutDialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CategoriesActivity extends Activity implements OnClickListener, OnItemClickListener {
	public static final String CMD_CATEGORIES_VIEW = "View";
	public static final String ACTION_SELECT_SEARCH = "SelectForSearch";
	public static final String CMD_SEARCH_PRODUCT = "SelectForSearchProduct";
	public static final String ACTION_SPECIAL = "SelectAllSpecial";
	public static final String EXTRA_SELECT_AREA = "SelectArea";
	private final static String EXTRA_KEY_WORD_SEARCH = "KeyWord";
	private static final String EXTRA_TYPE = "viewType";
	private ListView lvParentCategory;
	private Vector<Category> parentCategory;
	private ProductCategoriesAdapterManager productCategoryAdapterManager = null;
	private String mAction = null;
	private Area mArea = null;
	private ProgressDialog mProgressDialog = null;
	private String[] keywords = null;
	private int type = ProductCategoriesAdapterManager.ADAPTER_CATEGORY;
	private static InUIThreadHandler mReturnCodeHandler = null;
	private TextView thinner_title;
	
	private ImageButton ib_title_na_menu;
	private ImageButton ib_title_logo_home;
	private ImageView iv_title_search_home;
	
	/*
	 * init Menu Tab
	 */
	private BuiltinService builtservice = null;
	private BuiltinServiceManager mBuiltinServiceManager = null;
	private DrawerLayout drawerLayoutMenuTab;
	private ListView mDrawerList;
	private MenuTabAdapter tabAdapter;
	
	

	public static void actionSelectSearch(Context context, Area area) {
		Intent i = new Intent(context, CategoriesActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra(EXTRA_SELECT_AREA, area);
		i.setAction(ACTION_SELECT_SEARCH);
		context.startActivity(i);
	}

	public static void actionSelectSearchProductCount(Context context, Area area, String keywords, String backtext) {
		Intent i = new Intent(context, CategoriesActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra(EXTRA_SELECT_AREA, area);
		i.putExtra(EXTRA_KEY_WORD_SEARCH, keywords);
		i.setAction(CMD_SEARCH_PRODUCT);

		context.startActivity(i);
	}

	public static void actionSpecial(Context context, Area area) {
		Intent i = new Intent(context, CategoriesActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra(EXTRA_SELECT_AREA, area);
		i.setAction(ACTION_SPECIAL);
		context.startActivity(i);
	}

	public static void launchCategoriesView(Activity context, Area area) {
		Intent i = new Intent(context, CategoriesActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra(EXTRA_SELECT_AREA, area);
		i.putExtra(EXTRA_TYPE, ProductCategoriesAdapterManager.ADAPTER_CATEGORY);
		i.setAction(CMD_CATEGORIES_VIEW);
		context.startActivityForResult(i, 1);
	}
	
	private void processIntent() {
		Intent i = getIntent();
		mAction = i.getAction();
		Bundle extras = i.getExtras();
		mArea = (Area) extras.get(EXTRA_SELECT_AREA);
		type = extras.getInt(EXTRA_TYPE);
	}

	private void initControls() {
		ib_title_na_menu = (ImageButton) findViewById(R.id.btn_title_na_menu);
		ib_title_logo_home = (ImageButton) findViewById(R.id.btn_title_logo_home);
		iv_title_search_home = (ImageView) findViewById(R.id.btn_title_search_home);
		ib_title_na_menu.setOnClickListener(this);
		ib_title_logo_home.setOnClickListener(this);
		iv_title_search_home.setOnClickListener(this);
		
		drawerLayoutMenuTab = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList=(ListView)findViewById(R.id.drawer_cate);
		
		thinner_title = (TextView) findViewById(R.id.tvPromotionName);
		thinner_title.setText(Html.fromHtml(CategoriesActivity.this.getResources().getString(R.string.ia_category)));
		lvParentCategory = (ListView) findViewById(R.id.lv_parent_categories);
		productCategoryAdapterManager = ProductCategoriesAdapterManager.getInstance(CategoriesActivity.this, parentCategory, mAction);
		lvParentCategory.setAdapter(productCategoryAdapterManager.getAdapterByType(type));
		lvParentCategory.setOnItemClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mBuiltinServiceManager = BuiltinServiceManager.getInstance(CategoriesActivity.this);
		builtservice = mBuiltinServiceManager.getBuitinService();
		initMenuData();
	}
	
	private void whatAction() {
		if (mAction.equals(CMD_SEARCH_PRODUCT)) {
			parentCategory = new Vector<Category>();
			showProgressDialog(getString(R.string.feeds_loading_feeds));
		} else if (mAction.equals(ACTION_SPECIAL)) {
			parentCategory = new Vector<Category>();
			showProgressDialog(getString(R.string.feeds_loading_feeds));
			Hashtable<String, Object> options = new Hashtable<String, Object>();
			options.put("Special", true);
		} else {
			parentCategory = IAManager.getInstance(this).getViewCategory("" + ProductCategoriesAdapterManager.ADAPTER_CATEGORY);
		}
		productCategoryAdapterManager.notifyDataChanged(parentCategory);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ia_parent_categories_view);
		mReturnCodeHandler = new InUIThreadHandler();
		processIntent();
		initControls();
		setKeywords();
		whatAction();
	}

	private void setKeywords() {
		mAction = getIntent().getAction();
		Bundle extras = getIntent().getExtras();
		if (mAction != null) {
			if (mAction.equals(CMD_SEARCH_PRODUCT)) {
				String strKeywords = (String) extras.get(EXTRA_KEY_WORD_SEARCH);
				keywords = strKeywords.split(" ");
			}
		}
	}

	private void showProgressDialog(final String message) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mProgressDialog = new ProgressDialog(CategoriesActivity.this);
				mProgressDialog.setMessage(message);
				mProgressDialog.show();
			}
		});
	}

	private void dismissProgressDialog() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mProgressDialog.dismiss();
			}
		});
	}
	
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_title_na_menu:
			if (drawerLayoutMenuTab.isDrawerOpen(mDrawerList)) {
				drawerLayoutMenuTab.closeDrawers();
			} else {
				drawerLayoutMenuTab.openDrawer(mDrawerList);
			}
			break;

		case R.id.btn_title_search_home:
			ProductsActivity.actionSearchGlobal(this);
			break;
		case R.id.btn_title_logo_home:
			HomeClassifeldActivity.actionLaunch(CategoriesActivity.this);
			break;
		default:
			break;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  //click menu content
		Category cate = parentCategory.get(position);
		if (mAction.equals(CMD_CATEGORIES_VIEW)) {
			onView(cate, position);
		} 
	}

	private void onView(Category cate, int position) {
		SubCategoriesActivity.getIntentView(this, cate, mArea, position); //nhay sang product_cate_3
	}


	private void initMenuData(){
		if (builtservice == null){
			tabAdapter = new MenuTabAdapter(CategoriesActivity.this, false,IMenuTabConfig.MENU_HOME);
		}else{
			tabAdapter = new MenuTabAdapter(CategoriesActivity.this, true,IMenuTabConfig.MENU_HOME);
			tabAdapter.setUsername(builtservice.getUserName());
		}
		mDrawerList.setAdapter(tabAdapter);
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (tabAdapter.getTabModel() != null){
					int index = tabAdapter.getTabModel().get(position).getMenutab_key();
					switch (index) {
					case IMenuTabConfig.MENU_HOME:
						drawerLayoutMenuTab.closeDrawers();
						CategoriesActivity.launchCategoriesView(CategoriesActivity.this, mArea);
						break;
					case IMenuTabConfig.MENU_AREA:
						drawerLayoutMenuTab.closeDrawers();
						ProvincesActivity.actionChangeCityLaunch(CategoriesActivity.this);
						break;
					case IMenuTabConfig.MENU_PROFILE_NAME:
						if(builtservice==null){
							drawerLayoutMenuTab.closeDrawers();
							LoginActivity.actionLaunch(CategoriesActivity.this);
						}else{
							drawerLayoutMenuTab.closeDrawers();
							UserProfileActivity.actionLaunch(CategoriesActivity.this);
						}

						break;
					case IMenuTabConfig.MENU_INTEREST:
						drawerLayoutMenuTab.closeDrawers();
						FavoritesActivity.actionViewFavorites(CategoriesActivity.this);
						break;
					case IMenuTabConfig.MENU_POSTED:
						drawerLayoutMenuTab.closeDrawers();
						FavoritesActivity.actionViewPosted(CategoriesActivity.this);
						break;
					case IMenuTabConfig.MENU_HISTORY:
						drawerLayoutMenuTab.closeDrawers();
						FavoritesActivity.actionViewHistory(CategoriesActivity.this);

						break;
					case IMenuTabConfig.MENU_LOGOUT:
						drawerLayoutMenuTab.closeDrawers();
						LogoutDialog dialog= new LogoutDialog(CategoriesActivity.this);
						dialog.show();
						break;

					case IMenuTabConfig.MENU_POST:
						drawerLayoutMenuTab.closeDrawers();
						PostProductActivity.actionLaunch(CategoriesActivity.this);
						break;

					default:
						break;
					}

				}
			}
		});

	}

	@Override
	protected void onStart() {
		super.onStart();
		mBuiltinServiceManager = BuiltinServiceManager.getInstance(CategoriesActivity.this);
		builtservice = mBuiltinServiceManager.getBuitinService();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == ProvincesActivity.ACTION_CHANGE_AREA && resultCode == RESULT_OK){
			mArea =  IAManager.getInstance(this).getMyArea();
			if (tabAdapter != null && mArea != null){
				for (int i=0;i<tabAdapter.getTabModel().size();i++){
					if (tabAdapter.getTabModel().get(i).getMenutab_key() == IMenuTabConfig.MENU_AREA){
						tabAdapter.getTabModel().get(i).setNameMenu(mArea.getName());
						break;
					}
				}
				tabAdapter.notifyDataSetChanged();
			}
		}
	}
	
}
